<?php
/**
 * 共用控制器
 */
class CommonAction extends Action{
	/**
	 * 判断用户的登录状态
	 */
	public function _initialize(){
		if(!isset($_SESSION['aid']) || !isset($_SESSION['adminname'])){
			redirect(U('Login/index'));
		}
	}
	/**
	 * Uploadify品牌上传处理
	 */
	Public function uploadBrand () {
		$upload = $this->_upload('Brand');
		if ($upload['status']) {
			$arr = array(
				'status' => 1,
				'path' => $upload['info']['savename']
				);
			echo json_encode($arr);
		} else {
			$arr = array(
				'status' => 0,
				'msg' => $upload['msg']
				);
			echo json_encode($arr);
		}
	}

	/**
	 * 上传列表页图片
	 */
	Public function uploadList () {
		$upload = $this->_upload('List', true, 1, 160, 160);
		if ($upload['status']) {
			echo json_encode(array(
				'status' => 1,
				'path' => $upload['info']['savename']
				));
		} else {
			echo json_encode(array(
				'status' => 0,
				'msg' => $upload['msg']
				));
		}
	}

	/**
	 * 商品图册上传
	 */
	Public function uploadPic () {
		$upload = $this->_upload('Picture', true, 2, '400,99', '400,99');
		if ($upload['status']) {
			echo json_encode(array(
				'status' => 1,
				'path' => $upload['info']
				));
		} else {
			echo json_encode(array(
				'status' => 0,
				'msg' => $upload['msg']
				));
		}
	}

	/**
	 * 商品介绍Ueditor图片上传
	 */
	Public function intro() {
		header("Content-Type: text/html; charset=utf-8");
    	error_reporting(E_ERROR | E_WARNING);
    	$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
    	$upload = $this->_upload('intro');
    	if ($upload['status']) {
    		echo json_encode(array(
    			'url' => $upload['info']['savename'],
    			'title' => $title,
    			'state' => 'SUCCESS'
    			));
    	} else {
    		echo json_encode(array('state' => $upload['msg']));
    	}
	}

	/**
	 * 商品售后服务Ueditor图片上传
	 */
	Public function service () {
		header("Content-Type: text/html; charset=utf-8");
    	error_reporting(E_ERROR | E_WARNING);
    	$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
    	$upload = $this->_upload('service');
    	if ($upload['status']) {
    		echo json_encode(array(
    			'url' => $upload['info']['savename'],
    			'title' => $title,
    			'state' => 'SUCCESS'
    			));
    	} else {
    		echo json_encode(array('state' => $upload['msg']));
    	}
	}

	/**
	 * 图片上传
	 * @param  [String]  $path   [图片保存文件夹名称，图片将统一保存到/Uploads/]
	 * @param  boolean $thumb  [是否生成略图，默认为FALSE]
	 * @param  [Integer]  $type   [生成缩略图方式（1：缩略原图，2：生生两张缩略图并保存原图）]
	 * @param  [Int/String]  $width  [缩略图宽度]
	 * @param  [Int/String]  $height [缩略图高度]
	 * @return [Arrar]          [上传图片信息]
	 */
	Private function _upload ($path, $thumb=false, $type=1, $width=null, $height=null) {
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类

		$upload->maxSize = 2000000;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');	//充许上传文件的类型
		$upload->uploadReplace = true;	//覆盖同名文件
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/' . $path . '/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存

		if ($thumb) {
			$upload->thumb = true;	//生成缩略图
			$upload->thumbMaxWidth = $width; //缩略图宽度
			$upload->thumbMaxHeight = $height;	//缩略图高度
			$upload->uploadReplace = true;	//覆盖原图
			$upload->thumbRemoveOrigin = false;
			$upload->thumbPath = './Uploads/' . $path . '/' . date('Y-m') . '/';
			$upload->thumbPrefix = 'medium_,mini_';	//缩略图前缀名

			if ($type == 1) {
				$upload->thumbPrefix = '';
			}
			
		}

		if ($upload->upload()) {
			$info = $upload->getUploadFileInfo();

			if ($type == 2) {
				$pic =  explode('/', $info[0]['savename']);
				return array(
					'status' => 1,
					'info' => array(
						'max' => $info[0]['savename'],
						'medium' => $pic[0] . '/medium_' . $pic[1],
						'mini' => $pic[0] . '/mini_' . $pic[1]
						)
					);
			}

			return array('status' => 1, 'info' => $info[0]);
		} else {
			return array('status' => 0, 'msg' => $upload->getErrorMsg());
		}
	}

}



?>